package com.yu.task.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.yu.task.globalResult.R;
import com.yu.task.pojo.Tag;
import com.yu.task.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/tag")
public class TagController {
    @Autowired
    private TagService tagService;

    @PostMapping("/insertTag")
    public R insertTag(@RequestBody Tag tag){
        tag.setCreateTime(new Date());
        tag.setUpdateTime(new Date());
        int i = tagService.insertTag(tag);
        if (i == 1) {
            return R.success("插入成功");
        }else if(i == 2){
            return R.failure(201,"名字重复");
        }
        return R.failure("插入失败");
    }

    @PostMapping("/updateTag")
    public R updateTag(@RequestBody Tag tag){
        tag.setUpdateTime(new Date());
        int i = tagService.updateTag(tag);
        if (i == 1) {
            return R.success("更新成功");
        }
        return R.failure("更新失败");
    }

    @PostMapping("/deleteTag")
    public R deleteTag(@RequestParam Long id){
        int i = tagService.deleteTag(id);
        if (i == 1) {
            return R.success("删除成功");
        }
        return R.failure("删除失败");
    }

    @GetMapping("/selectTag")
    public R selectTag(){
       List<Tag> list = tagService.selectTag();
        if (list != null) {
            return R.success(list);
        }
        return R.failure("获取失败");
    }


}
